﻿
buildDiv("UserSelect");
function SelectUser(NameID, ValueID, MaxNum, DeptorUser, DeptName, DeptID) {
    initJobArea(NameID, ValueID, MaxNum, DeptorUser, 'UserSelect', DeptName, DeptID);
    displayLayer("UserSelect");
    $("PID").focus();
}
function GetUserTable(keyword) {
    return ZX.WebApp.Crm.UserSelect.GetUserInfo(keyword).value;
}
function GetDeptTable() {
    return ZX.WebApp.Crm.UserSelect.GetDeptInfo().value;
}
function GetInfoTableByDept(DeptValue, userordept, UseDefault) {
    return ZX.WebApp.Crm.UserSelect.GetInfoTableByDept(DeptValue, userordept, UseDefault).value;
}
function GetDeptForSelected(DeptValue) {
    return ZX.WebApp.Crm.UserSelect.GetDeptForSelected(DeptValue).value;
}
function GetDeptByUser(userID) {
    return ZX.WebApp.Crm.UserSelect.GetDeptByUserID(userID).value;
}

function $(id) { return document.getElementById(id) }
function ShowDig(userID, userName, isChoose) {
    $("lblUserName").innerText = $("ctl00_ContentPlace_hdfchooseUserName").value = userName;
    $("lblUserID").innerText = $("ctl00_ContentPlace_hdfchooseUserID").value = userID;
    $("ctl00_ContentPlace_lnkDiagShow").click();
    return false;
}

//buildDiv();
function buildDiv(DivName) {
    var htmlDiv = '';
    htmlDiv = '<div id=' + DivName + '  style="width:500px;border:1px;position:absolute;left:10px;top:-165px;visibility:hidden;z-index:100000;'
    	+ 'filter:progid:DXImageTransform.Microsoft.DropShadow(Color=#BFBFBF,OffX=4,OffY=4);">';

    htmlDiv = htmlDiv + '</div>';
    document.write(htmlDiv);

    var htmlIfrm = '<iframe  id="DivShim"  scrolling="no"  style="width:500px;position:absolute;top:0px; left:0px; display:none;"> </iframe>  ';
    document.write(htmlIfrm);
}

//div move by mouse Begin
var x0 = 0, y0 = 0, x1 = 0, y1 = 0;
var moveable = false;
//开始拖动;
function startDrag(DivName) {
    var obj = document.getElementById(DivName);
    var IfrRef = document.getElementById('DivShim');

    IfrRef.style.width = obj.offsetWidth - 5;
    IfrRef.style.height = obj.offsetHeight;
    IfrRef.style.top = obj.style.top;
    IfrRef.style.left = obj.style.left;
    IfrRef.style.zIndex = obj.style.zIndex - 1;
    IfrRef.style.display = "block";

    obj.onmousemove = drag;
    obj.onmouseup = stopDrag;
    if (event.button == 1) {
        //锁定标题栏;
        obj.setCapture();
        //记录鼠标和层位置;
        x0 = event.clientX;
        y0 = event.clientY;
        x1 = parseInt(obj.style.left);
        y1 = parseInt(obj.style.top);
        //记录颜色;
        //改变风格;
        obj.style.left = x1 + 3;
        obj.style.top = y1 + 3;
        moveable = true;
    }
}
//拖动;
function drag() {
    if (moveable) {
        this.style.left = x1 + event.clientX - x0;
        this.style.top = y1 + event.clientY - y0;

        var IfrRef = document.getElementById('DivShim');
        IfrRef.style.left = x1 + event.clientX - x0;
        IfrRef.style.top = y1 + event.clientY - y0;
    }
}
//停止拖动;
function stopDrag() {
    if (moveable) {
        this.style.left = this.style.left;
        this.style.top = this.style.top;
        this.releaseCapture();

        var IfrRef = document.getElementById('DivShim');
        IfrRef.style.left = this.style.left;
        IfrRef.style.top = this.style.top;
        moveable = false;
    }
}

//End

function displayLayer(DivName) {
    var dv = document.getElementById(DivName);
    GetCenterXY_ForLayer(dv);

    //    var frdv = document.getElementById("DivShim");
    //	GetCenterXY_ForLayer(frdv);
    //	
    dv.style.visibility = "visible";
}

function hiddenLayer(DivName) {

    document.getElementById(DivName).style.visibility = "hidden";
    var IfrRef = document.getElementById('DivShim');
    IfrRef.style.display = "none";
}

//将悬浮层的位置定位在屏幕中央
function GetCenterXY_ForLayer(objdiv) {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {
        // all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }

    var windowHeight;
    if (self.innerHeight) {
        // all except Explorer
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        // Explorer 6 Strict Mode
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowHeight = document.body.clientHeight;
    }

    var marginTop = yScroll + (windowHeight - objdiv.offsetHeight) / 2;

    objdiv.style.top = marginTop + "px";
    objdiv.style.left = (document.body.offsetWidth - 400) / 2 + "px";

    //显示iframe
    var dv = document.getElementById('DivShim');
    dv.style.height = objdiv.clientHeight;
    dv.style.top = marginTop + "px";
    dv.style.left = (document.body.offsetWidth - 400) / 2 + "px";
    dv.style.display = "block";
}


function initJobArea(objName, objValue, state, DeptorUser, DivName, DeptName, DeptID) {
    var htmlDiv = '';
    var htmlDiv1 = '';
    var deptoruser = DeptorUser == 'User' ? '人员' : '部门';
    htmlDiv = htmlDiv + '<table style="width:500px" border="1" cellspacing="0" cellpadding="0" bgcolor="#999999" >';
    htmlDiv = htmlDiv + '<tr><td colspan="2"  style="width:450px;height:27px;color:#fff;font-weight:bold;vertical-align:middle;text-align:left;background:url(../../Images/crm/images1.jpg);cursor:move"  onmousedown= "startDrag(\'' + DivName + '\');" >&nbsp;&nbsp;'
    + '<img src="../../Images/crm/icon02.gif">&nbsp;&nbsp' + deptoruser + '选择 <a onclick="javascript:hiddenLayer(\'' + DivName + '\')" style="color:#fff;cursor:hand; float:right;">[关闭]</a></td>'
    + '</tr>';
    htmlDiv = htmlDiv + '<tr><td colspan="2" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">';
    htmlDiv = htmlDiv + '<tr><td><input type=hidden value="' + state + '" id="stype"><input type=hidden value="31210100" id="sdept"><input type=hidden value="" id="sdept_level"></td></tr>';
    htmlDiv = htmlDiv + '<tr><td style="text-align:left"><label id="lab_stype" style="color: blue;padding-left:20px;">[只能选择 ' + state + ' 个]</label></td><td colspan="2"  style="text-align:right;padding-right:10px">选择部门：';
    htmlDiv = htmlDiv + '<select name="ly" id = "ddlDept" onchange="changedept(this,\'' + DeptorUser + '\');"><option value="ALL"> 中旭通讯簿</option></select></td>';
    htmlDiv = htmlDiv + '</tr><td id="TDPID"  style="text-align:right;">按姓名或登录账号查询：<br /><input style="width: 206px;border-width: 1px;" id="PID" onkeydown="EnterToTable();"  onkeyup="inputOnchange()"  /></td><td></td><td style="text-align:left;padding-left:10px"><br/>已选择' + deptoruser + '：</td>';
    htmlDiv = htmlDiv + '<tr><td   style="text-align:right"> <select style="display: none;" id="sele2" size="12" name="D1"></select> ';
    htmlDiv = htmlDiv + '<select  style="font-size: 9pt; width:208px" id="sele" size="10" ondblclick="return sele_ondblclick()"  onkeypress="press();" name="D2"></select></td>';
    htmlDiv = htmlDiv + '<td> <br /><br /><input type="button" value=" -> " onclick="return btnAdd_onclick()" /><br/><br/>  <input type="button" value=" <- " onclick="return btnDel_onclick()" /><br /></td>';
    htmlDiv = htmlDiv + '<td><select id="selected" size="10" style="width:208px; font-size: 9pt;" ondblclick="return selected_ondblclick()"  name="D3">   </select></td>';
    htmlDiv = htmlDiv + '</tr><tr style="height:20px"></tr><tr>';
    htmlDiv = htmlDiv + '<td colspan="3"  style="text-align:center"><input type="button" style="cursor:hand;" value=" 确 定 " onclick="GetSelectedValue(\'' + objName + '\',\'' + objValue + '\',\'' + DivName + '\',\'' + DeptName + '\',\'' + DeptID + '\');" id="button1" name="button1"  /> ';
    htmlDiv = htmlDiv + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" style="cursor:hand;" value=" 取 消 " onclick="javascript:hiddenLayer(\'' + DivName + '\')" /></td>';
    htmlDiv = htmlDiv + '</tr><tr style="height:20px"></tr>';
    htmlDiv = htmlDiv + ' <td style="height: 100%;">';
    htmlDiv = htmlDiv + ' </td></tr><tr></tr></table>';
    htmlDiv = htmlDiv + htmlDiv1 + '</td></tr><tr><td colspan="2" style="height:4px" bgcolor="#7C88DC"></td></tr></table>';


    var dv = $(DivName);
    dv.innerHTML = htmlDiv;
    if (DeptorUser == 'User') {
        $("TDPID").style.visibility = 'visible';
    }
    else {
        $("TDPID").style.visibility = 'hidden';
    }
    FillDept(DeptorUser, objValue);
    updateselected(objValue, DeptorUser);
}


function FillDept(DeptorUser, objValue) {
    var dt = GetDeptTable();
    var ddlDept = $("ddlDept");
    for (var i = 0; i < dt.Rows.length; i++) {
        ddlDept.options.add(new Option(dt.Rows[i]["DeptName"], dt.Rows[i]["DeptValue"]));
    }
    if (DeptorUser == 'Dept' && $(objValue).value != '') {
        var str = $(objValue).value;
        str = str.split(';');
        str = str[0];
        for (var j = str.length; j > 0 && str.charAt(j - 1) == "0"; j--);
        str = str.substring(0, (j - 2));
        var strlength = 8 - str.length;
        for (i = 0; i < strlength; i++) {
            str += "0";
        }
        ddlDept.value = str;
        changedept(ddlDept, DeptorUser);
    }
    if ($(objValue).value == '') {
        changedept(ddlDept, DeptorUser, 'True');
    }
}


function FillUserName(keyword) {
    var dt = GetUserTable(keyword);
    var sele = $("sele");
    var selected = $("selected");
    sele.length = 0;
    for (var i = 0; i < dt.Rows.length; i++) {
        var name = dt.Rows[i]["UName"] + "(" + dt.Rows[i]["UserID"] + ")";
        var value = dt.Rows[i]["UName"] + "(" + dt.Rows[i]["UserID"] + ")";
        sele.options.add(new Option(name, value));
    }
}


function press() {
    var sele = $("sele");
    var k = event.keyCode;
    if (k == 13) { sele_ondblclick(); return -1; }

    if (k == 37 || k == 38) { sele.selectedIndex = sele.selectedIndex - 1; return -1; } //上一个
    if (k == 39 || k == 40) { sele.selectedIndex = sele.selectedIndex + 1; return -1; } //下一个

}


function EnterToTable() {
    var k = event.keyCode;
    if (k == 13) {
        sele_ondblclick();
        return event.keyCode = 9;
    }
}



function inputOnchange() {
    if (press() == -1) return;
    var s2 = $("PID").value

    var s1;
    var i;
    var j;
    var sele = $("sele");
    if (s2 == '') return;
    FillUserName(s2);
    for (i = 0; i < sele.length; i++) {
        s1 = sele.options(i).text;
        if (s2.toUpperCase() == s1.substr(0, s2.length).toUpperCase()) {
            if (i == sele.selectedIndex) return;
            if (i > 11) {
                i = i + 11;
                sele.selectedIndex = i;
                i = i - 11;
                sele.selectedIndex = i;
            }
            else
                sele.selectedIndex = i;
            //alert(i);
            return;
        }
    }
    sele.selectedIndex = 0
    sele.selectedIndex = -1
}


function sele_ondblclick() {
    var sele = $("sele");
    var selected = $("selected");
    var stype = $("stype");
    var PID = $("PID");
    var lab_stype = $("lab_stype");
    if (sele.selectedIndex != -1) {

        if (selected.options.length >= stype.value) {
            lab_stype.style.color = 'red';
            return;
        }
        var oItem = document.createElement("OPTION");
        oItem.value = sele.options(sele.selectedIndex).value;
        oItem.text = sele.options(sele.selectedIndex).text;

        if (!isChosen(oItem.value)) {
            selected.add(oItem); PID.select();
        } //PID.focus();
    }
}


function isChosen(sUserCode) {
    var selected = $("selected");
    var i;
    for (i = 0; i < selected.options.length; i++) {
        if (selected.options(i).value == sUserCode) {
            return (true);
        }
    }
    return (false);
}



function GetSelectedValue(objName, objValue, DivName, DeptName, DeptID) {
    var i, Textvalue, TextName, text2;
    Textvalue = TextName = '';
    var selected = $("selected");

    for (i = 0; i < selected.options.length; i++) {
        text2 = selected.options(i).value;
        TextName = TextName + text2.substring(0, text2.indexOf('(')) + ';'
        text2 = text2.substring(text2.indexOf('(') + 1);
        text2 = text2.substring(0, text2.indexOf(')'));
        Textvalue = Textvalue + text2 + ';'

    }
    intstrLength = Textvalue.length;

    if (intstrLength > 0) {
        Textvalue = Textvalue.substring(0, intstrLength - 1);
    }
    intstrLength = TextName.length;

    if (intstrLength > 0) {
        TextName = TextName.substring(0, intstrLength - 1);
    }

    $(objValue).value = Textvalue;
    $(objName).value = TextName;
    hiddenLayer(DivName);

    if ($(DeptName) != null || $(DeptID) != null) {
        //	    if  ($(DeptName).value !="")
        //	    {return false;}
        var Dept = GetDeptByUser(Textvalue);
        var spDept = Dept.split(';');
        $(DeptName).value = spDept[0];
        $(DeptID).value = spDept[1];
    }
}





function selected_ondblclick() {
    var selected = $("selected");
    if (selected.selectedIndex != -1) {
        selected.remove(selected.selectedIndex);
    }
}



function btnAdd_onclick() {
    var selected = $("selected");
    var sele = $("sele");
    var stype = $("stype");
    var lab_stype = $("lab_stype");
    var i;
    var oItem;
    for (i = 0; i < sele.options.length; i++) {
        oItem = sele.options(i);
        if (oItem.selected) {
            if (!isChosen(oItem.value)) {
                if (selected.options.length >= stype.value) {
                    lab_stype.style.color = 'red';
                    return;
                }

                oItem = document.createElement("OPTION");
                oItem.value = sele.options(i).value;
                oItem.text = sele.options(i).text;
                selected.add(oItem);
            }
        }
    }
}



function btnDel_onclick() {
    var selected = $("selected");
    var i;
    for (i = selected.options.length - 1; i >= 0; i--) {
        if (selected.options(i).selected) {
            selected.remove(i);
        }
    }
}


function btnOK_onclick() {

}


function changedept(obj, userordept, UseDefault) {
    var dt = GetInfoTableByDept(obj.value, userordept, UseDefault);
    var sele = $("sele");
    sele.length = 0;
    if (userordept == 'User') {
        for (var i = 0; i < dt.Rows.length; i++) {
            var name = dt.Rows[i]["UName"] + "(" + dt.Rows[i]["UserID"] + ")";
            var value = dt.Rows[i]["UName"] + "(" + dt.Rows[i]["UserID"] + ")";
            sele.options.add(new Option(name, value));
        }
    }
    else {
        for (var i = 0; i < dt.Rows.length; i++) {
            var name = dt.Rows[i]["DEPT_NAME"] + "(" + dt.Rows[i]["DEPT"] + ")";
            var value = dt.Rows[i]["DEPT_NAME"] + "(" + dt.Rows[i]["DEPT"] + ")";
            sele.options.add(new Option(name, value));
        }
    }
}



function updateselected(TextValue, userordept) {
    sid = $(TextValue).value;
    if (sid == '') {
        return;
    }
    re = /；/gi
    sid = sid.replace(re, sid);
    strSelectAarry = sid.split(';');
    for (j = 0; j < strSelectAarry.length; j++) {
        if (strSelectAarry[j] == '')
        { continue; }
        if (userordept == 'User') {
            var dt = GetUserTable(strSelectAarry[j]);
            for (var i = 0; i < dt.Rows.length; i++) {
                var name = dt.Rows[i]["UName"] + "(" + dt.Rows[i]["UserID"] + ")";
                var value = dt.Rows[i]["UName"] + "(" + dt.Rows[i]["UserID"] + ")";
                $('selected').options.add(new Option(name, value));
            }
        }
        else {
            var dt = GetDeptForSelected(strSelectAarry[j]);
            for (var i = 0; i < dt.Rows.length; i++) {
                var name = dt.Rows[i]["DEPT_NAME"] + "(" + dt.Rows[i]["DEPT"] + ")";
                var value = dt.Rows[i]["DEPT_NAME"] + "(" + dt.Rows[i]["DEPT"] + ")";
                $('selected').options.add(new Option(name, value));
            }
        }
    }

}